/*
This program runs the primary impact equations to estimate the impact of the intervention on various outcomes, including:
Table 2: Woman knowledge
Table 3: Household characteristics
Table 4: Health practices with new child
Table 5: Development measures
Table 6: Anthropometric measures
Table 8: Long-run follow-up results
Table 9: Child practices

To run the regressions, we use an Excel file that indicates the outcomes, input dataset, sample to use, and additional control variables.
It then loops over all of the specifications to run the simple impact regressions (specified in Equation 1)
*/


local folder "C:\Users\mlevere\OneDrive - Mathematica\Documents\Projects\Nepal\"

clear all
set more off

import excel using "`folder'/Programs/control_file_all_regs.xlsx", clear first

// Establish standard controls for women and child regressions
local std_controls_child "male child_tot_months2-child_tot_months20 school_e underweight_b underweight_no_b stunted_b stunted_no_b wasted_b wasted_no_b hh_head_male_e hh_head_age_e num_hh_members_e"
local std_controls_women "hh_head_male_b hh_head_age_b2-hh_head_age_b5 num_hh_members_b"

local maxregs = _N

ren *, l
gen obs = _n

egen tablenum = group(tablename)
sort tablenum obs
by tablenum, sort: gen tableobs = _n
gen regobs = _n

// Localize everything in the Excel control file
forvalues i = 1/`maxregs' {
local tablename`i' = tablename[`i']
local tableobs`i' = tableobs[`i']
local outvar`i' = outvar[`i']
local ifstat`i' = ifstat[`i']
local addl_controls`i' = controls[`i']
local data`i' = data[`i']
}

foreach type in women child child_fu women_fu {
sum regobs if data == "`type'", detail
local `type'_min = r(min)
local `type'_max = r(max)
}


// Establish the postfile that will contain all the results for control group means and differences
capt postclose tables

tempfile tables
postfile tables str32(tablename) str32(outvar) tableobs controls controlmean diff se pval using `tables', replace

foreach i of numlist 1/`maxregs' {

// Four main datasets that we use for the regression: woman level/child-level, and at endline/follow-up
if `i' == `women_min' {
use "`folder'/Data/women_endline_reg_data", clear
}

if `i' == `child_min' {
use "`folder'/Data/child_endline_reg_data", clear
}
if `i' == `child_fu_min' {
use "`folder'/Data/child_follow_up_reg_data", clear
}
if `i' == `women_fu_min' {
use "`folder'/Data/women_follow_up_reg_data", clear
}


// Regression without controls
forvalues j = 0/1 {

// Establish control variables -- either nothing or the standard controls plus whatever additional ones are specified in the Excel doc
if `j' == 1 local controls "`std_controls_`data`i''' `addl_controls`i''"
if `j' == 0 local controls ""

// Run the regression: the outcome on indicators for info/cash, including control variables, throwing out those flagged observations, and further subsetting as needed
qui svy: regress `outvar`i'' info_vdc cash_vdc `controls' if flag == 0 `ifstat`i''

if (`tableobs`i'' == 1){
outreg2 using "`folder'/Output/`tablename`i''_controls`j'.doc", word tex(pr) nocons label dec(3) keep(info_vdc cash_vdc) replace
}
else {
outreg2 using "`folder'/Output/`tablename`i''_controls`j'.doc", word tex(pr) nocons label dec(3) keep(info_vdc cash_vdc)
}

// Test for a difference between the cash and info groups
qui lincom cash_vdc - info_vdc
local diff = r(estimate)
local se = r(se)
local pval = r(p)
local stars ""
if `pval' < 0.1 local stars "`stars'*"
if `pval' < 0.05 local stars "`stars'*"
if `pval' < 0.01 local stars "`stars'*"

// Get the control group mean
qui sum `outvar`i'' if flag == 0 `ifstat`i'' & control_vdc == 1
local mean = r(mean)

post tables ("`tablename`i''") ("`outvar`i''") (`tableobs`i'') (`j') (`mean') (`diff') (`se') (`pval')

}

}
postclose tables

use `tables', clear

replace diff = round(diff,0.001)
replace controlmean = round(controlmean,0.001)
replace se = round(se,0.001)
replace pval = round(pval,0.001)

levelsof tablename, local(tabnames)

foreach table of local tabnames {
forvalues j = 0/1 {
qui sum tableobs if tablename == "`table'"
local maxtable = `r(max)'

local lets "A B C D E F G H I J K"

local alltables ""
forvalues v = 1/`maxtable'{

local let : word `v' of `lets'

qui estpost su diff se pval controlmean if tableobs == `v' & controls == `j' & tablename == "`table'"
qui est store `let'

local alltables "`alltables' `let'"

}
 
	qui esttab `alltables' using "`folder'/Output/`table'_diff_controls`j'.rtf", replace ///
		cells((mean(fmt(3)))) noobs  /*label booktabs*/ nonum /*collabels(none) gaps f*/ plain   
}
}

